package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sneakerappforme1105.GoodsDetailActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.bean.OrderItem;
import com.example.sneakerappforme1105.util.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderItemAdapter extends RecyclerView.Adapter {
    private ArrayList<OrderItem> mList;
    private Context mContext;
    private HashMap<Long, Goods> mGoodsMap = new HashMap<>();


    public OrderItemAdapter(Context mContext, List<OrderItem>mList, HashMap<Long,Goods>mGoodsMap){
        this.mList = (ArrayList<OrderItem>) mList;
        this.mContext = mContext;
        this.mGoodsMap = mGoodsMap;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        // 根据单条数据的xml得到视图
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_orderitem,viewGroup,false);
        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        OrderItem item = mList.get(position);
        ItemHolder holder = (ItemHolder) viewHolder;
        holder.bindBean(item);
    }

    private class ItemHolder extends RecyclerView.ViewHolder {
        private final TextView tv_name;
        private final TextView tv_desc;
        private final TextView tv_price;
        private final ImageView iv_thumb;
        private TextView tv_ItemNum;


        public ItemHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name3);
            tv_desc = itemView.findViewById(R.id.tv_desc3);
            tv_price = itemView.findViewById(R.id.tv_price3);
            iv_thumb = itemView.findViewById(R.id.iv_thumb3);
            tv_ItemNum = itemView.findViewById(R.id.tv_ItemNum3);
        }

        void bindBean(OrderItem item){
            Goods goods = mGoodsMap.get(item.getGoodsId()); // 当前购物车项目对应的商品
            tv_name.setText(goods.getName());
            tv_desc.setText(goods.getDesc() +" " +"尺码 "+item.getSize());
            tv_price.setText("￥"+String.valueOf(GoodsDetailActivity.formatDouble(item.getPrice()) ));   // 单价
//            iv_thumb.setImageResource(goods.getPicRes());
            ImageLoader.loadImage(mContext, goods.getPicPath(), iv_thumb);
            tv_ItemNum.setText("x"+String.valueOf(item.getNum()));
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
